package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/7/23 15:47
 * @Description:
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {


	@Autowired
	MemberDao memberDao;

	@Autowired
	OrderDao orderDao;
	/**
	 *  @描述：获取运营统计数据
	 */
	public Map<String, Object> getBusinessReportData() {
		//reportDate:null,
		//		todayNewMember :0,
		//		totalMember :0,
		//		thisWeekNewMember :0,
		//		thisMonthNewMember :0,
		//		todayOrderNumber :0,
		//		todayVisitsNumber :0,
		//		thisWeekOrderNumber :0,
		//		thisWeekVisitsNumber :0,
		//		thisMonthOrderNumber :0,
		//		thisMonthVisitsNumber :0,
		//		hotSetmeal :[
		//{name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
		//{name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
        //            ]
		Map<String, Object> map = new HashMap<>();
		LocalDate now = LocalDate.now();
		// 今天的日期字符串
		String today = getDateStr(now);
		// 本周一的日期字符串
		String monday = getDateStr(now.with(DayOfWeek.MONDAY));
		// 本月一号的日期字符串
		String firstDay = getDateStr(now.with(TemporalAdjusters.firstDayOfMonth()));
		// 查询会员信息
		Integer todayNewMember = memberDao.findMemberCountByDate(today);
		Integer totalMember = memberDao.findMemberTotalCount();
		Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
		Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay);
		// 查询预约信息
		Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
		Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
		Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);
		Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
		Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay);
		Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay);
		List<Map> hotSetmeal = orderDao.findHotSetmeal();


		map.put("reportDate",today);
		map.put("todayNewMember",todayNewMember);
		map.put("totalMember",totalMember);
		map.put("thisWeekNewMember",thisWeekNewMember);
		map.put("thisMonthNewMember",thisMonthNewMember);
		map.put("todayOrderNumber",todayOrderNumber);
		map.put("thisWeekOrderNumber",thisWeekOrderNumber);
		map.put("thisMonthOrderNumber",thisMonthOrderNumber);
		map.put("todayVisitsNumber",todayVisitsNumber);
		map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
		map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
		map.put("hotSetmeal",hotSetmeal);



		return map;
	}
    /**
     *  @描述：生成日期字符串
     */
	private String getDateStr(LocalDate now) {
		return DateTimeFormatter.ofPattern("yyyy/MM/dd").format(now);
	}
}
